﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITJZ.BitHelper.APIModel;
using ITJZ.BitHelper.Local.UI.IComponent;
using System.Windows.Forms;

namespace ITJZ.BitHelper.Local.UI
{
    /// <summary>
    /// 客户端行为的指挥类
    /// </summary>
    public class Commander
    {
        /// <summary>
        /// 采用单例模式
        /// </summary>
        private readonly  static Commander _commander = new Commander();
        protected Commander()
        {
            //受保护的构造函数
        }
        public static Commander Create() { return _commander; }



        User user = null;//当前用户

        ISearchKnowledge iSearch = null;
        IEditorKnowledge iEditor = null;
        IShowKnowledge iShow = null;


        //填充组件
        UserControl search = Component.SearchComFactory.Create();
        UserControl edit = Component.EditorComFactory.Create();
        UserControl show = Component.ShowComFactory.Create();

        /// <summary>
        /// 程序启动
        /// </summary>
        public void AppRun()
        {

            iSearch = search as ISearchKnowledge;
            iEditor = edit as IEditorKnowledge;
            iShow = show as IShowKnowledge;

            //完成事件订阅
            iSearch.SelectChange += new EventHandler(search_SelectChange);
            iEditor.Save += new EventHandler(editor_Save);
            iEditor.Close += new EventHandler(editor_Close);

            edit.Visible = false;

            //
            frmMain main = new frmMain();
            main.splitContainer1.Panel1.Controls.Add(search);
            main.splitContainer1.Panel2.Controls.Add(edit);
            main.splitContainer1.Panel2.Controls.Add(show);            



            Application.Run(main);
            

        }

        #region 事件处理函数：搜索组件
        private void search_SelectChange(object sender, EventArgs e)
        {
            string guid = iSearch.KnowledgeGuid;

            //获取到这个知识信息
            API.Knowledge akl = new ITJZ.BitHelper.Local.API.Knowledge();
            APIModel.Knowledge knowledge = akl.GetKnowledge(guid);

            iShow.Show(knowledge);
        } 
        #endregion

        #region 事件处理函数：知识编辑器组件
        private void editor_Save(object sender, EventArgs e)
        {
            Knowledge lk = iEditor.CurKnowledge;

            //提交这个知识到服务器并在本地数据库保存副本
            API.Knowledge api = new ITJZ.BitHelper.Local.API.Knowledge();
            api.Add(lk);

            iEditor.Hide();//隐藏编辑组件
            iShow.Show(lk);//显示刚才编辑的内容
        }
        private void editor_Close(object sender, EventArgs e)
        {
            iEditor.Hide();
            iShow.Show();
        } 
        #endregion
    }





}
